package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.EchartCount;
import com.wn.class30.service.CountService;
import com.wn.class30.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
public class CountController {
    @Resource
    private CountService countService;
    @RequestMapping("/houtai/page/selectIndexOrderCount")
    public Result selectOrderCount(String startDate,String endDate) throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (startDate != null && !"".equals(startDate) && endDate != null && !"".equals(endDate)) {
            List<EchartCount> list = new ArrayList<>();
            EchartCount echartCount = new EchartCount();
            echartCount.setName(startDate);
            list.add(echartCount);
            Date date1 = simpleDateFormat.parse(startDate);
            Date date2 = simpleDateFormat.parse(endDate);
            long day = (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);
            log.info("day-------------------:" + day);
            for (int i = 0; i < day; i++) {
                EchartCount echartCountEach = new EchartCount();
                Calendar c = Calendar.getInstance();
                c.setTime(date1);
                c.add(Calendar.DAY_OF_MONTH, i + 1);
                Date time = c.getTime();
                String format = simpleDateFormat.format(time);
                echartCountEach.setName(format);
                list.add(echartCountEach);
            }

            List<Integer> orderCount = countService.selectOrderCount(list);

            for (int i = 0; i < list.size(); i++) {
                list.get(i).setNum(orderCount.get(i));
            }
            for (EchartCount count : list) {
                System.out.println(count);
            }
            return new Result("200", "success", null, list);
        } else {
            List<EchartCount> list = new ArrayList<>();
            String endDates = simpleDateFormat.format(new Date());
            EchartCount echartCount = new EchartCount();
            echartCount.setName(endDates);
            list.add(echartCount);
            for (int i = 0; i < 6; i++) {
                EchartCount echartCountEach = new EchartCount();
                Calendar c = Calendar.getInstance();
                c.setTime(simpleDateFormat.parse(endDates));
                c.add(Calendar.DAY_OF_MONTH,  -i-1);
                Date time = c.getTime();
                String format = simpleDateFormat.format(time);
                echartCountEach.setName(format);
                list.add(echartCountEach);
            }
            List<Integer> orderCount = countService.selectOrderCount(list);
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setNum(orderCount.get(i));
            }
            Collections.reverse(list);
            return new Result("200", "success", null, list);
        }
    }
    @RequestMapping("/houtai/page/selectIndexUserCount")
    public Result selectUserCount(String startDate,String endDate) throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (startDate != null && !"".equals(startDate) && endDate != null && !"".equals(endDate)) {
            List<EchartCount> list = new ArrayList<>();
            EchartCount echartCount = new EchartCount();
            echartCount.setName(startDate);
            list.add(echartCount);
            Date date1 = simpleDateFormat.parse(startDate);
            Date date2 = simpleDateFormat.parse(endDate);
            long day = (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);

            for (int i = 0; i < day; i++) {
                EchartCount echartCountEach = new EchartCount();
                Calendar c = Calendar.getInstance();
                c.setTime(date1);
                c.add(Calendar.DAY_OF_MONTH, i + 1);
                Date time = c.getTime();
                String format = simpleDateFormat.format(time);
                echartCountEach.setName(format);
                list.add(echartCountEach);
            }

            List<Integer> orderCount = countService.selectUserCount(list);

            for (int i = 0; i < list.size(); i++) {
                list.get(i).setNum(orderCount.get(i));
            }
            for (EchartCount count : list) {
                System.out.println(count);
            }
            return new Result("200", "success", null, list);
        } else {
            List<EchartCount> list = new ArrayList<>();
            String endDates = simpleDateFormat.format(new Date());
            EchartCount echartCount = new EchartCount();
            echartCount.setName(endDates);
            list.add(echartCount);
            for (int i = 0; i < 6; i++) {
                EchartCount echartCountEach = new EchartCount();
                Calendar c = Calendar.getInstance();
                c.setTime(simpleDateFormat.parse(endDates));
                c.add(Calendar.DAY_OF_MONTH,  -i-1);
                Date time = c.getTime();
                String format = simpleDateFormat.format(time);
                echartCountEach.setName(format);
                list.add(echartCountEach);
            }
            Collections.reverse(list);
            List<Integer> orderCount = countService.selectUserCount(list);
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setNum(orderCount.get(i));
            }

            return new Result("200", "success", null, list);
        }
    }
}
